package com.rc.saas.tenant.controller.log;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.msg.TenantNotice;
import com.rc.saas.tenant.search.msg.TenantNoticeSearch;
import com.rc.saas.tenant.search.tenant.TenantOperateLogSearch;
import com.rc.saas.tenant.service.log.TenantLogService;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.log.TenantOperateLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * Created by sven on 2019/11/11
 */
@Controller
@RequestMapping("/tenantOperateLog")
public class TenantOperateLogController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(TenantOperateLogController.class);

    private String prefix = "/templates/log";

    @Autowired
    private TenantLogService tenantLogService;


    /**
     * 跳转首页列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return prefix + "/tenantOperateLog";
    }

    /**
     * 日志列表
     *
     * @param search
     * @return
     */
    @RequestMapping(value = "/listTenantOperateLog", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listTenantOperateLog(TenantOperateLogSearch search) {
        if (StringUtils.isBlank(search.getStartTime()) && StringUtils.isBlank(search.getEndTime())) {
            return null;
        }

        PageImpl<TenantOperateLog> pageInfo = tenantLogService.findPageTenantOperateLog(search);
        if (pageInfo != null) {
            logger.info("pageInfo.getTotal()).intValue():{}", pageInfo.getTotalElements());
        }

        //PageInfo转BootstrapPage
        WebPageInfo bootstrapPage = new WebPageInfo(pageInfo.getContent(), Long.valueOf(pageInfo.getTotalElements()).intValue());
        return bootstrapPage;
    }

    /**
     * 导出操作日志列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(TenantOperateLogSearch search, HttpServletResponse response) {
        try {
            ExportUtils.setDefaultPaging(search);
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            PageImpl<TenantOperateLog> page = tenantLogService.findPageTenantOperateLog(search);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("操作日志列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), TenantOperateLog.class).sheet("操作日志列表").doWrite(page.getContent());
            return null;
        } catch (Exception e) {
            logger.error("导出操作日志列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }
}
